// pages/addSchedule/index.js
const app = getApp()
import { getScheduleAdd } from '../../api/schedule.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttoncolor:false,
    name:'',//参赛名称
    nameerr:false,
    date: '',//开赛日期
    dateerr: false,
    multiIndex: [0, 0, 0],
    multiArray: [],
    time: {
      start: 0, // 开始时间是今天的前0天
      end: 730 // 结束时间是今天的730天后 （2年）
    },
    start: '2020-01-01',
    end: '2023-09-01'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { start, end } = this.data.time
    let day1 = new Date().valueOf();
    let startTime = new Date(day1 - (24 * 60 * 60 * 1000 * start));
    let endTime = new Date(day1 + (24 * 60 * 60 * 1000 * end));
    let start1 = startTime.getFullYear() + '-' + (startTime.getMonth() + 1) + '-' + startTime.getDate();
    let end1 = endTime.getFullYear() + '-' + (endTime.getMonth() + 1) + '-' + endTime.getDate();
    this.setData({ start: start1, end: end1, navHeight: app.globalData.navHeight * 2, tabHeight: app.globalData.navHeight * 2 - app.globalData.navTop })
  },

  onShow() {
    app._getAddressInfo(data => {
      this.setData({multiArray: data[0] })
      this.bindMultiPickerColumnChange({ detail: { column: 0, value: 0 } })
    })
  },

  bindMultiPickerColumnChange(e) {
    let multiIndex = this.data.multiIndex
    multiIndex[e.detail.column] = e.detail.value
    this.setData({ ...app._processingAreaData(multiIndex) })
  },
  bindReplaceName: function (e) {
    this.setData({name: e.detail.value})
    this.check()
  },
  //日期
  bindDateChange: function(e) {
    this.setData({date: e.detail.value})
    this.check()
  },
  check() {
    let result2 = this.error(this.data.date, 'dateerr')
    let result3 = this.error(this.data.name, 'nameerr')
    this.setData({ buttoncolor: result2 == "1" && result3 == "1" })
  },
  error(value,valueerr){
    if(value==null || value==''){
      this.setData({
        valueerr :true
      })
      return "0"
    }else{
      this.setData({
        valueerr :false
      })
      return "1"
    }
  },
  submit() {
    let { name, date, multiArray, multiIndex } = this.data
    let result2 = this.error(date,'dateerr')
    let result3 = this.error(name,'nameerr')
    if(result2=="1"&&result3=="1"){
      date = date.replace(/-/g, '/')
      let address = '';
      if (multiArray[0][multiIndex[0]]) {
        address += multiArray[0][multiIndex[0]]
      }
      if (multiArray[1][multiIndex[1]]) {
        address += (',' + multiArray[1][multiIndex[1]])
      }
      if (multiArray[2][multiIndex[2]]) {
        address += (',' + multiArray[2][multiIndex[2]])
      }
      getScheduleAdd({
        matchId: 0,
        matchName: name,
        timeStart: date,
        shortAddress: address
      }).then(res => {
        if (res.code == 0) {
          wx.showToast({title: '添加成功'})
          wx.navigateBack(1)
        } else {
          wx.showToast({title: res.msg||'添加失败',icon:'none'})
        }
      })
      this.setData({buttoncolor:true})
    }else{
        this.setData({
          buttoncolor:false
        })
    }
  }
})